package com.canbot.u05.activity.training;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.training.Step;

import java.util.ArrayList;
import java.util.List;

/**
 * 培训主持模式-------单个任务设置界面
 */

public class TrainingTaskSettingActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private String stepName;

        private String taskName;

        private int stepId;

        private TextView textView;

        private String path;

        private String actionId;

        private String mPicPath;

        private boolean isTts;

        private SharedPreferences sp;

        private List<Step> stepOld =new ArrayList<>();

        private String stepData;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_training_host_task_setting);

                sp = getSharedPreferences("training_host", Context.MODE_PRIVATE);
                stepName = getIntent().getStringExtra("stepName");
                taskName = getIntent().getStringExtra("taskName");
                stepId = getIntent().getIntExtra("stepId", 0);
                if (TextUtils.isEmpty(taskName)) {
                        stepData = getIntent().getStringExtra("stepData");
                }
                if(!TextUtils.isEmpty(taskName)){
                        String stepList = sp.getString(taskName, null);
                        stepOld = JSON.parseArray(stepList, Step.class);
                }else{
                        stepOld = JSON.parseArray(stepData, Step.class);
                }
                initView();
        }

        private void initView() {
                findViewById(R.id.bt_back).setOnClickListener(this);
                findViewById(R.id.bt_image).setOnClickListener(this);
                findViewById(R.id.bt_action).setOnClickListener(this);
                findViewById(R.id.bt_audio).setOnClickListener(this);
                textView = (TextView) findViewById(R.id.text_title);
                textView.setText(stepName);
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {

                        case R.id.bt_back:
                                finish();
                                break;
                        case R.id.bt_image:

                                startActivityForResult(new Intent(TrainingTaskSettingActivity.this, TrainingHostImageSettingActivity.class)
                                        .putExtra("oldPicPath", stepOld.get(stepId).getPicPath())
                                        .putExtra("isUpdate", "isUpdate"), 130);

                                break;
                        case R.id.bt_action:

                                startActivityForResult(new Intent(TrainingTaskSettingActivity.this, TrainingActionSettingActivity.class)
                                        .putExtra("oldActionId", stepOld.get(stepId).getActionId())
                                        .putExtra("isUpdate", "isUpdate"), 131);
                                break;
                        case R.id.bt_audio:

                                startActivityForResult(new Intent(TrainingTaskSettingActivity.this, TrainingAudioSettingActivity.class)
                                        .putExtra("oldPath", stepOld.get(stepId).getAudioPath())
                                        .putExtra("isAudioFile", stepOld.get(stepId).ismIsAudioFile())
                                        .putExtra("isUpdate", "isUpdate"), 132);
                                break;
                }

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                if (data == null) {
                        return;
                }

                if(resultCode == RESULT_OK){
                        if (requestCode == 130 ) {//图片
                                mPicPath = data.getStringExtra("mPicPath");
                                stepOld.get(stepId).setPicPath(mPicPath);
                                if(!TextUtils.isEmpty(taskName)){
                                        sp.edit().putString(taskName, JSON.toJSONString(stepOld)).apply();
                                }else{
                                        sp.edit().putString("step", JSON.toJSONString(stepOld)).apply();
                                }
                                return;
                        }

                        if (requestCode == 131 ) {//动作
                                actionId = data.getStringExtra("actionId");
                                stepOld.get(stepId).setActionId(actionId);
                                if(!TextUtils.isEmpty(taskName)){
                                        sp.edit().putString(taskName, JSON.toJSONString(stepOld)).apply();
                                }else{
                                        sp.edit().putString("step", JSON.toJSONString(stepOld)).apply();
                                }
                                return;
                        }

                        if (requestCode == 132 ) {//音频
                                path = data.getStringExtra("path");
                                isTts = data.getBooleanExtra("isTts", false);
                                stepOld.get(stepId).setAudioPath(path);
                                stepOld.get(stepId).setAudioFile(isTts);
                                if(!TextUtils.isEmpty(taskName)){
                                        sp.edit().putString(taskName, JSON.toJSONString(stepOld)).apply();
                                }else{
                                        sp.edit().putString("step", JSON.toJSONString(stepOld)).apply();
                                }
                        }

                }
        }


}
